//流水线作业调度问题
#include <iostream>
#include <algorithm>
using namespace std;
struct Schedule
{
    int a,b,index;
};
bool cmp(Schedule& a,Schedule& b)
{
    return min(a.a,b.b)<min(a.b,b.a);
}
int main()
{
    int n;
    cin>>n;
    Schedule schedule[n];
    for(int i=0;i<n;i++)
    {
        cin>>schedule[i].a>>schedule[i].b;
        schedule[i].index=i;
    }
    sort(schedule,schedule+n,cmp);

    int tempx=0,tempy=0;
    for(int i=0;i<n;i++)
    {
        tempx+=schedule[i].a;
        tempy=max(tempx,tempy)+schedule[i].b;
    }
    cout<<tempy<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<schedule[i].index<<" ";
    }
    cout<<endl;
    return 0;
}

